package com.itheima.controller;

import com.aliyuncs.exceptions.ClientException;
import com.itheima.constant.MessageConstant;
import com.itheima.constant.RedisMessageConstant;
import com.itheima.entity.Result;
import com.itheima.utils.SMSUtils;
import com.itheima.utils.ValidateCodeUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import redis.clients.jedis.JedisPool;
import org.springframework.util.StringUtils;

import javax.sound.midi.Soundbank;
import java.util.HashMap;
import java.util.Set;

/**
 * @Author: bowen
 * @Date: 2019/7/19 15:08
 * @Description:验证码
 */
@RestController
@RequestMapping("/validateCode")
public class ValidateCodeController {

    @Autowired
    JedisPool jedisPool;


    /**
     * @描述：发送预约验证码
     */
    @RequestMapping("/send4Order")
    public Result send4Order(String telephoneNo) {
        // 生成验证码
        Integer generateValidateCode = ValidateCodeUtils.generateValidateCode(6);
        String code = String.valueOf(generateValidateCode);
        boolean judge = judge(telephoneNo, RedisMessageConstant.SENDTYPE_ORDER);
        if (judge) {
            // 发送验证码
            //try {
            //    SMSUtils.sendShortMessage(SMSUtils.ORDER_VALIDATE_CODE, telephoneNo, code);
            //} catch (Exception e) {
            //    e.printStackTrace();
            //    return new Result(false, MessageConstant.SEND_VALIDATECODE_FAIL);
            //}
            // 用Redis缓存验证码
            jedisPool.getResource().setex(telephoneNo + RedisMessageConstant.SENDTYPE_ORDER, 5 * 60, code);
            return new Result(true, MessageConstant.SEND_VALIDATECODE_SUCCESS);
        }
        return new Result(false, "操作频繁，请稍后再试！");
    }

    /**
     * @描述：发送登录验证码
     */
    @RequestMapping("/send4Login")
    public Result send4Login(String telephoneNo) {
        // 生成验证码
        Integer generateValidateCode = ValidateCodeUtils.generateValidateCode(6);
        String code = String.valueOf(generateValidateCode);
        // 发送验证码
        try {
            SMSUtils.sendShortMessage(SMSUtils.LOGIN_VALIDATE_CODE, telephoneNo, code);
        } catch (ClientException e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.SEND_VALIDATECODE_FAIL);
        }
        // 用Redis缓存验证码
        jedisPool.getResource().setex(telephoneNo + RedisMessageConstant.SENDTYPE_LOGIN, 5 * 60, code);
        return new Result(true, MessageConstant.SEND_VALIDATECODE_SUCCESS);
    }

	/*9.短信防刷机制
对使用同一个手机号码发送同一类型验证码的时候，系统可以对这个手机号码是否重复发送进行限制，
    五分钟之内不能超出限制则进行报错（如：系统繁忙，请稍后再试）。
*/

    /**
     * @param telephoneNum
     * @return
     */
    private boolean judge(String telephoneNum, String sendType) {
        HashMap<String, Long> map = new HashMap<>();
        long time = System.currentTimeMillis();
        map.put(telephoneNum, time);
        String numInRedis = jedisPool.getResource().get(telephoneNum + sendType);
        long l = System.currentTimeMillis() - time;
        System.out.println(l);
        return StringUtils.isEmpty(numInRedis) || (System.currentTimeMillis() - time) > (1000 * 60 * 5);
    }
}
